package com.tanhua.server.service;

import cn.hutool.core.collection.CollUtil;
import com.github.tobato.fastdfs.domain.conn.FdfsWebServer;
import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.tanhua.autoconfig.templates.OssTemplate;
import com.tanhua.commons.utils.Constants;
import com.tanhua.dubbo.api.SmallVideosApi;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.model.db.UserInfo;
import com.tanhua.model.mongo.Video;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.VideoVo;
import com.tanhua.server.interceptor.UserHolder;

import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class SmallVideosService {
    @Autowired
    private OssTemplate ossTemplate;
    @DubboReference
    private SmallVideosApi smallVideosApi;
    @Autowired
    private FastFileStorageClient client;
    @Autowired
    private FdfsWebServer webServer;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @DubboReference
    private UserInfoApi userInfoApi;
    //上传视频
    public void smallVideos(MultipartFile videoThumbnail, MultipartFile videoFile) throws IOException {
        //上传阿里云
        String picUrl = ossTemplate.upload(videoThumbnail.getInputStream(), videoThumbnail.getOriginalFilename());

        //上传fastdfs
        //名字
        String filename = videoFile.getOriginalFilename();
        int index = filename.lastIndexOf(".");
        //后缀
        filename = filename.substring(index + 1);
        StorePath storePath = client.uploadFile(videoFile.getInputStream(), videoFile.getSize(), filename, null);
        //拼接地址
        String videoUrl = webServer.getWebServerUrl() + storePath.getFullPath();
        //构造对象 保存
        Video video = new Video();
        video.setCreated(System.currentTimeMillis());
        video.setUserId(UserHolder.getUserId());
        video.setText("hello");
        video.setPicUrl(picUrl);
        video.setVideoUrl(videoUrl);
        smallVideosApi.save(video);


    }
    //视频列表
    public PageResult findVideos(Integer page, Integer pagesize) {
        //从redis中查询推荐的视频
        String key = Constants.VIDEOS_RECOMMEND;
        String value = redisTemplate.opsForValue().get(key);
        //判断是否存在
        List<Video> list = new ArrayList<>();
        if (StringUtils.isNotEmpty(value)){
            String[] ids = value.split(",");
            list= smallVideosApi.findByIds(ids,page,pagesize);
        }else {
            //不存在查询全部
            list = smallVideosApi.findAll(page,pagesize);
        }
        if (CollUtil.isEmpty(list)){
            return new PageResult();
        }

        //查询发布人信息
        //获取ids
        List<Long> ids = CollUtil.getFieldValues(list, "userId", Long.class);
        Map<Long, UserInfo> map = userInfoApi.findByIds(ids);
        List<VideoVo> vos = new ArrayList<>();
        for (Video video : list) {
            Long userId = video.getUserId();
            UserInfo userInfo = map.get(userId);
            VideoVo vo = VideoVo.init(userInfo, video);
            vos.add(vo);
        }
        return new PageResult(page,pagesize,0l,vos);
    }
}
